/**
 * 
 */
package com.swim.natatoriumgoods.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.swim.natatoriumgoods.entity.NatatoriumGoods;
import com.swim.webuser.service.WebUserService;
import com.swim.natatoriumgoods.dao.NatatoriumGoodsDao;
import com.jeesite.modules.file.entity.FileUpload;
import com.jeesite.modules.file.utils.FileUploadUtils;
import com.jeesite.modules.sys.utils.UserUtils;

/**
 * 游泳馆商品Service
 * @author tql
 * @version 2021-03-14
 */
@Service
@Transactional(readOnly=true)
public class NatatoriumGoodsService extends CrudService<NatatoriumGoodsDao, NatatoriumGoods> {
	
	@Value("${server.context-path}")
	private String contextPath;

    @Autowired
    private WebUserService webUserService;
	
	/**
	 * 获取单条数据
	 * @param natatoriumGoods
	 * @return
	 */
	@Override
	public NatatoriumGoods get(NatatoriumGoods natatoriumGoods) {
		NatatoriumGoods result = super.get(natatoriumGoods);
		if (result != null) {
			//获取商品图片
			List<FileUpload> fileUploadListOne = FileUploadUtils.findFileUpload(result.getId(), "natatoriumGoods_image");
			if (!CollectionUtils.isEmpty(fileUploadListOne)) {
				List<String> natatoriumGoodsImageUrlList = new ArrayList<String>();	//商品图片url集合
				natatoriumGoodsImageUrlList.add(contextPath + "/userfiles/fileupload/" + fileUploadListOne.get(0).getFileEntity().getFilePath() + fileUploadListOne.get(0).getFileEntity().getFileId() + "." + fileUploadListOne.get(0).getFileEntity().getFileExtension());
				result.setNatatoriumGoodsImageUrlList(natatoriumGoodsImageUrlList);
			}
		}
		return result;
	}
	
	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param natatoriumGoods
	 * @return
	 */
	@Override
	public Page<NatatoriumGoods> findPage(Page<NatatoriumGoods> page, NatatoriumGoods natatoriumGoods) {
		Page<NatatoriumGoods> pageList = super.findPage(page, natatoriumGoods);
		List<NatatoriumGoods> natatoriumGoodsList = pageList.getList();
		if (!CollectionUtils.isEmpty(natatoriumGoodsList)) {
			for (NatatoriumGoods natatoriumGoodsOne : natatoriumGoodsList) {
				//获取商品图片
				List<FileUpload> fileUploadListOne = FileUploadUtils.findFileUpload(natatoriumGoodsOne.getId(), "natatoriumGoods_image");
				if (!CollectionUtils.isEmpty(fileUploadListOne)) {
					List<String> natatoriumGoodsImageUrlList = new ArrayList<String>();	//商品图片url集合
					natatoriumGoodsImageUrlList.add(contextPath + "/userfiles/fileupload/" + fileUploadListOne.get(0).getFileEntity().getFilePath() + fileUploadListOne.get(0).getFileEntity().getFileId() + "." + fileUploadListOne.get(0).getFileEntity().getFileExtension());
					natatoriumGoodsOne.setNatatoriumGoodsImageUrlList(natatoriumGoodsImageUrlList);
				}
			}
		}
		pageList.setList(natatoriumGoodsList);
		return pageList;
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param natatoriumGoods
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(NatatoriumGoods natatoriumGoods) {
		if (natatoriumGoods.getGoodsStatus() == 1) {// 商品状态 1表示已下架 2表示上架中
			natatoriumGoods.setDownSellTime(new Date());		// 下架时间
		} else {
			natatoriumGoods.setUpSellTime(new Date());		// 上架时间
		}
        String natatoriumInfoId = webUserService.getNatatoriumInfoIdByUserCode(UserUtils.getUser().getUserCode());
        natatoriumGoods.setNatatoriumInfoId(natatoriumInfoId);
		super.save(natatoriumGoods);
		// 保存上传图片
		FileUploadUtils.saveFileUpload(natatoriumGoods.getId(), "natatoriumGoods_image");
	}
	
	/**
	 * 更新状态
	 * @param natatoriumGoods
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(NatatoriumGoods natatoriumGoods) {
		super.updateStatus(natatoriumGoods);
	}
	
	/**
	 * 删除数据
	 * @param natatoriumGoods
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(NatatoriumGoods natatoriumGoods) {
		super.delete(natatoriumGoods);
	}
	
}